package edu.task.service.impl;

import edu.task.common.result.RestResult;
import edu.task.mapper.Mapper;
import edu.task.model.SysRole;
import edu.task.model.SysRoleMenu;
import edu.task.model.SysUser;
import edu.task.model.SysUserRole;
import edu.task.model.vo.SysRoleVO;
import edu.task.model.vo.SysUserVO;
import edu.task.repository.SysRoleMenuRepository;
import edu.task.repository.SysRoleRepository;
import edu.task.service.SysRoleService;
import edu.task.utils.BeanUtils;
import edu.task.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.transaction.Transactional;
import java.util.*;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    SysRoleRepository sysRoleRepository;

    @Autowired
    SysRoleMenuRepository sysRoleMenuRepository;

    @Autowired
    Mapper mapper;

    /**
     * 查询所有角色
     * @return 角色列表
     */
    @Override
    public List<SysRole> selectRoleAll() {
        return sysRoleRepository.findAll();
    }

    /**
     * 根据用户 ID获取角色选择框列表
     * @param userId 用户 ID
     * @return 选中角色 ID 列表
     */
    @Override
    public List<Long> selectRoleListByUserId(Long userId) {
        return sysRoleRepository.selectRoleIdsByUserId(userId);
    }

    @Override
    @Transactional
    public boolean insertUser(SysRoleVO role) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(role, sysRole);

        Date date = new Date();
        sysRole.setCreateTime(date);
        sysRole.setUpdateTime(date);

        mapper.insertRole(sysRole);
        role.setRoleId(sysRole.getRoleId());
        insertRoleMenu(role);
        return null != sysRole.getRoleId();
    }

    @Transactional
    public int insertRoleMenu(SysRoleVO role) {
        Long[] menus = role.getMenuIds();
        if (StringUtils.isNull(menus) || menus.length == 0) {
            return 0;
        }
        Long[] roleIds = new Long[menus.length];
        Long roleId = role.getRoleId();
        for (Long menuId : menus) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenuRepository.save(sysRoleMenu);
        }
        Arrays.fill(roleIds, role.getRoleId());
        return 1;

    }

    @Override
    @Transactional
    public int deleteRoleByIds(Long[] roleIds) {
        return sysRoleRepository.deleteUserByIds(roleIds);
    }

    @Override
    public List<SysRole> selectRoleList() {
        return sysRoleRepository.selectRoleList();
    }

    @Override
    @Transactional
    public boolean updateRole(SysRoleVO role) {
        Long roleId = role.getRoleId();
        Optional<SysRole> op = sysRoleRepository.findById(roleId);
        if (!op.isPresent()) {
            return false;
        }
        // 删除角色与菜单关联
        sysRoleRepository.deleteRoleMenuByRoleId(roleId);
        SysRole sysRole = op.get();
        BeanUtils.copyPropertiesIgnoreNull(role, sysRole);
        //更新角色信息
        sysRoleRepository.save(sysRole);
        //新增角色与菜单关联
        insertRoleMenu(role);
        return null != sysRole.getRoleId();
    }

    @Override
    public SysRole selectRoleById(Long roleId) {
        SysRole sysrole = new SysRole();
        sysrole.setRoleId(roleId);
        sysrole.setDelFlag("0");
        return sysRoleRepository.findOne(Example.of(sysrole)).get();
    }

    @Override
    public Set<String> selectRolePermissionByUserId(Long userId) {
        List<SysRole> perms=sysRoleRepository.selectRoleByUserId(userId);
        Set<String> permsSet=new HashSet<>();
        for (SysRole perm:perms) {
            if (StringUtils.isNotNull(perm)) {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }


}
